package com.centaline.androidsalesblog.activities.sale;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.model.LatLng;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.base.BaseRentListActivity;
import com.centaline.androidsalesblog.constants.CommonConstant;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.model.businissobject.DropBo;
import com.centaline.androidsalesblog.rx.database.RxDataBase;
import com.centaline.androidsalesblog.rx.database.SearchDataFunc1;
import com.centaline.androidsalesblog.widgets.dropdown.MoreDrop;
import com.centaline.androidsalesblog.widgets.dropdown.SalePriceDrop;
import com.centaline.androidsalesblog.widgets.dropdown.SingleDrop;
import com.trello.rxlifecycle.ActivityEvent;

import java.util.ArrayList;
import java.util.Locale;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func4;
import rx.schedulers.Schedulers;

/**
 * 小区在租列表
 * Created by guilin on 16/1/26.
 */
public class EsfRentOfPriceActivity extends BaseRentListActivity {

    private SalePriceDrop salePriceDrop;
    private SingleDrop postRoomDrop;
    private SingleDrop areaDrop;
    private MoreDrop moreDrop;

    @Override
    protected void findViews() {
        setUniversalToolbar(R.string.title_esf_rent_of_price);
        super.findViews();
    }

    @Override
    protected void initViews() {
        super.initViews();
        final double samePrice = getIntent().getDoubleExtra(ExtraConstant.Price, 0);
        if (samePrice != 0) {
            postListApi.setMin(String.format(Locale.CHINA, "%.0f", samePrice * 0.8));
            postListApi.setMax(String.format(Locale.CHINA, "%.0f", samePrice * 1.2));
            salePriceDrop.setMaxAndMin(String.format(Locale.CHINA, "%.0f", samePrice * 1.2),
                    String.format(Locale.CHINA, "%.0f", samePrice * 0.8));
            setTab(0, String.format(Locale.CHINA, "%s-%s",
                    String.format(Locale.CHINA, "%.0f", samePrice * 0.8),
                    String.format(Locale.CHINA, "%.0f元", samePrice * 1.2)));
        }
        postListApi.setEstCode(getIntent().getStringExtra(ExtraConstant.CestCode));
    }

    @Override
    protected void initDrop() {
        dropDownView.initTabs(R.array.rentEsfOfPrice);
        salePriceDrop = new SalePriceDrop(dropDownView, this);
        salePriceDrop.setPriceUnit(1);
        postRoomDrop = new SingleDrop(dropDownView, this);
        areaDrop = new SingleDrop(dropDownView, this);
        moreDrop = new MoreDrop(dropDownView, this);
        dropDownView.addDrops(salePriceDrop, postRoomDrop, areaDrop, moreDrop);

        RxDataBase.getSearchData("Rent")
                .map(new SearchDataFunc1(30))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<ArrayList<DropBo>>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Action1<ArrayList<DropBo>>() {
                    @Override
                    public void call(ArrayList<DropBo> dropBos) {
                        salePriceDrop.init(dropBos);
                    }
                });
        RxDataBase.getSearchData("Room")
                .map(new SearchDataFunc1(40))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<ArrayList<DropBo>>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Action1<ArrayList<DropBo>>() {
                    @Override
                    public void call(ArrayList<DropBo> dropBos) {
                        dropBos.get(0).setSelected(true);
                        postRoomDrop.init(dropBos);
                    }
                });
        RxDataBase.getSearchData("Area")
                .map(new SearchDataFunc1(60))
                .observeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<ArrayList<DropBo>>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Action1<ArrayList<DropBo>>() {
                    @Override
                    public void call(ArrayList<DropBo> dropBos) {
                        areaDrop.init(dropBos);
                    }
                });
        Observable.combineLatest(
                RxDataBase.getSearchData("RentOrderby").map(new SearchDataFunc1(100)),
                RxDataBase.getSearchData("Direction").map(new SearchDataFunc1(50)),
                RxDataBase.getSearchData("Fitment").map(new SearchDataFunc1(70)),
                RxDataBase.getSearchData("RentTag").map(new SearchDataFunc1(80)),
                new Func4<ArrayList<DropBo>, ArrayList<DropBo>, ArrayList<DropBo>, ArrayList<DropBo>,
                                        ArrayList<DropBo>>() {
                    @Override
                    public ArrayList<DropBo> call(ArrayList<DropBo> dropBos, ArrayList<DropBo> dropBos2,
                                                  ArrayList<DropBo> dropBos3, ArrayList<DropBo> dropBos4) {
                        final ArrayList<DropBo> list = new ArrayList<>();
                        DropBo dropBo = new DropBo(100);
                        if (dropBos.size()>0) {
                            dropBo.setText("排序");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos);
                            list.add(dropBo);
                        }
                        if (dropBos2.size()>0) {
                            dropBo = new DropBo(50);
                            dropBo.setText("朝向");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos2);
                            list.add(dropBo);
                        }
                        if (dropBos3.size()>0) {
                            dropBo = new DropBo(70);
                            dropBo.setText("装修情况");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos3);
                            list.add(dropBo);
                        }
                        if (dropBos4.size()>0) {
                            dropBo = new DropBo(80);
                            dropBo.setText("特色");
                            dropBo.setSelected(true);
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos4);
                            list.add(dropBo);
                        }
                        return list;
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<ArrayList<DropBo>>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Action1<ArrayList<DropBo>>() {
                    @Override
                    public void call(ArrayList<DropBo> dropBos) {
                        moreDrop.init(dropBos);
                    }
                });

    }

    @Override
    protected void locationInfo(BDLocation bdLocation) {
        switch (bdLocation.getLocType()) {
            case 61:
            case 161:
                LatLng latLng = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
                rentListFragment.setLocation(latLng);
                postListApi.setLatLng(latLng);
                request();
                break;
            default:
                rentListFragment.setLocation(null);
                rentListFragment.stopRefresh(null);
                break;
        }
    }
}
